package io.github.harmonly.service.impl;

import io.github.harmonly.entity.activity.Activity;
import io.github.harmonly.entity.activity.ActivityComment;
import io.github.harmonly.mapper.ActivityMapper;
import io.github.harmonly.service.ActivityService;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Resource
    private ActivityMapper mapper;

    private HashMap<Integer, Timer> activities;

    @PostConstruct
    public void initActivities() {
        activities = new HashMap<>();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                getActivities().forEach(activity -> add(activity));
            }
        }, 3000);
    }

    public void add(Activity activity) {
        if (activity.getEnd().getTime() < new Date().getTime()) {
            deleteActivity(activity.getCid());
            return;
        }
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                deleteActivity(activity.getCid());
            }
        }, activity.getEnd().getTime());
        activities.put(activity.getCid(), timer);
    }

    public void remove(int cid) {
        if (activities.containsKey(cid)) {
            activities.get(cid).cancel();
            activities.remove(cid);
        }
    }

    @Override
    public List<Activity> getActivitiesByKeyWord(String keyword) {
        return mapper.getActivitiesByKeyWord(keyword);
    }

    @Override
    public List<Activity> getActivitiesByAid(int aid) {
        return mapper.getActivitiesByAssociation(aid);
    }

    @Override
    public List<Activity> getActivities() {
        return mapper.getActivities();
    }

    @Override
    public Activity getActivity(int cid) {
        return mapper.getActivity(cid);
    }

    @Override
    public boolean addActivity(Activity activity) {
        boolean result = mapper.addActivity(activity) == 1;
        if (result) getActivities().forEach(v -> {
            if (!activities.containsKey(v.getCid())) add(v);
        });
        return result;
    }

    @Override
    public boolean deleteActivity(int cid) {
        boolean result = mapper.deleteActivity(cid) == 1;
        remove(cid);
        return result;
    }

    @Override
    public boolean editActivity(Activity activity) {
        return mapper.editActivity(activity) == 1;
    }

    @Override
    public List<ActivityComment> getComments() {
        return mapper.getComments();
    }

    @Override
    public List<ActivityComment> getCommentsByCid(int cid) {
        return mapper.getCommentsByCid(cid);
    }

    @Override
    public ActivityComment getComment(int ccid) {
        return mapper.getComment(ccid);
    }

    @Override
    public boolean addComment(ActivityComment comment) {
        System.out.println(comment);
        return mapper.addComment(comment) == 1;
    }

    @Override
    public boolean deleteComment(int ccid) {
        return mapper.deleteComment(ccid) == 1;
    }

    @Override
    public boolean editComment(ActivityComment comment) {
        return mapper.editComment(comment) == 1;
    }
}
